package com.vaadin.incubator.bugrap.tests.model;

import java.util.List;

import junit.framework.TestCase;

import org.junit.Before;

import com.vaadin.incubator.bugrap.model.facade.FacadeFactory;
import com.vaadin.incubator.bugrap.model.facade.FacadeUtil;
import com.vaadin.incubator.bugrap.model.projects.Project;
import com.vaadin.incubator.bugrap.model.projects.ProjectVersion;
import com.vaadin.incubator.bugrap.model.reports.Comment;
import com.vaadin.incubator.bugrap.model.reports.Report;
import com.vaadin.incubator.bugrap.model.users.Reporter;

public class TestBase extends TestCase {

	/**
	 * @throws java.lang.Exception
	 */
	@Override
	@Before
	public void setUp() throws Exception {

		// Register a test facade (see persitance.xml for details
		FacadeUtil.setFacade(FacadeUtil.TESTING_FACADE_NAME, null);

		// Clear the database
		clearDB();
	}

	private void clearDB() {

		// Comments
		List<Comment> comments = FacadeFactory.getFacade().list(Comment.class);
		FacadeFactory.getFacade().deleteAll(comments);

		// Reports
		List<Report> reports = FacadeFactory.getFacade().list(Report.class);
		FacadeFactory.getFacade().deleteAll(reports);

		// Report versions
		List<ProjectVersion> versions = FacadeFactory.getFacade().list(
				ProjectVersion.class);
		FacadeFactory.getFacade().deleteAll(versions);

		// Projects
		List<Project> projects = FacadeFactory.getFacade().list(Project.class);
		FacadeFactory.getFacade().deleteAll(projects);

		// Reporters
		List<Reporter> users = FacadeFactory.getFacade().list(Reporter.class);
		FacadeFactory.getFacade().deleteAll(users);
	}
}
